package xyz.kongbai121.modules.numcount.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import xyz.kongbai121.common.utils.Result;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信登录相关
 */
@RestController
@Api(tags = "登录相关")
public class WeCharLoginController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/login")
    public Result<JSONObject> login(String code){
        HashMap<String, String> map = new HashMap<>();
        map.put("appid", "wx70ce8a294da99e90");
        map.put("secret", "989669239131425ee9523495b7ff332e");
        map.put("js_code", code);
        map.put("grant_type", "authorization_code");
        String json = restTemplate.getForObject("https://api.weixin.qq.com/sns/jscode2session" +
                        "?appid={appid}" +
                        "&secret={secret}" +
                        "&js_code={js_code}" +
                        "&grant_type={grant_type}",
                String.class,
                map
        );
        JSONObject body = JSON.parseObject(json, JSONObject.class);
        return Result.ok(body);
    }
}
